<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Server-Sent Events Example</title>
</head>
<body>

<h1>Server-Sent Events Example</h1>
<div id="messages"></div>

<script>
    // 创建一个 EventSource 对象，连接到 /sse 接口
    const eventSource = new EventSource("/sse/sse");

    // 每当接收到数据时，处理该事件
    eventSource.onmessage = function(event) {
        const messagesDiv = document.getElementById('messages');
        const message = document.createElement('p');
        message.textContent = event.data;
        messagesDiv.appendChild(message);
    };

    // 错误处理
    eventSource.onerror = function(error) {
        console.error("EventSource failed:", error);
    };
</script>

</body>
</html>